$(function(){
    let layer = layui.layer
    function getComment(){
        $("#tb").empty()
        $.ajax({
            url: "admin/comments",
            success: function(res){
                if(res.status!==0){
                    return layer.msg(res.message)
                }
                // console.log(res)
                // layer.msg(res.message)
                $(template("template",res)).appendTo($("#tb"))
            }
        })
    }
    getComment()

    //模板引擎的过滤函数，用于获取时间
    template.defaults.imports.getTime = function(data){
        return data.replace(/T.*/i,"")
    }

    //删除按钮发送ajax
    $("#tb").on("click", ".delBtn", function () {
        let id = $(this).attr("data-id")
        // console.log(id)
            layer.confirm('确认要删除吗?', { icon: 3, title: '提示' }, function (index) {
                $.ajax({
                    url: "admin/comments/" + id,
                    type: "delete",
                    success: function (res) {
                        if (res.status !== 0) {
                            return layer.msg(res.message)
                        }
                        layer.msg(res.message)
                        //删除成功在次获取一下
                        getComment()
                    }
                })

                layer.close(index);
            });
        })
})